package com.smart.wap.bodys;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.digest.DigestUtil;
import com.smart.manager.finance.entities.enums.PayPlatform;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;

/**
 * @author Administrator
 */
public class PaymentBody {


    @Data
    @Accessors(chain = true)
    public static class CreateOrderBody {

        @ApiModelProperty(value = "充值价目表ID", required = true)
        @NotNull(message = "充值价目表ID 不能为空")
        private Long tariffId;


        @ApiModelProperty(value = "11位时间戳", required = true)
        @NotNull(message = "时间戳 不能为空")
        private Long timestamp;

        @ApiModelProperty(value = "支付平台", required = true)
        @NotNull(message = "支付平台 不能为空")
        private PayPlatform payPlatform;

        @ApiModelProperty(value = "应用ID", required = true)
        @NotNull(message = "支付平台的应用ID 不能为空")
        private String appId;


        @ApiModelProperty(value = "签名算法:大写32位MD5(SMART-支付平台代码-应用ID-充值价目表ID-时间戳) ", required = true)
        @Length(min = 32, max = 32, message = "订单创建签名 格式错误")
        private String sign;

        /**
         * 验证签名
         */
        public void checkSign() {
            String sourceStr = String.format("SMART-%s-%s-%s-%s",payPlatform.name(),appId,tariffId, timestamp);
            String sign2 = DigestUtil.md5Hex(sourceStr).toUpperCase();
            Assert.equals(this.sign, sign2, "创建订单签名错误");
            long abs = Math.abs(DateUtil.currentSeconds() - timestamp);
            Assert.isTrue(abs <= 300L, "设备时间异常:请校验当前设备的时间后再进行充值");
        }


    }

    @Data
    @Accessors(chain = true)
    public static class AgentOrderBody {

        @ApiModelProperty(value = "充值价目表ID", required = true)
        @NotNull(message = "充值价目表ID 不能为空")
        private Long levelId;

    }

    @Data
    public static class AgentTxBody {

        @ApiModelProperty(value = "提现金额", required = true)
        @NotNull(message = "提现金额 不能为空")
        private BigDecimal amount;

    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @ApiModel("UploadAssemblerBase64")
    public static class Base64 implements Serializable {
        /**
         * base64字符串 需代文件头
         */
        @NotBlank(message = "Base64字符串不能为空")
        private String base64;
    }


}
